package wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.sns.base.SnsConfig;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.dialog.AlertDialogCreator;
import com.alstudio.view.listener.AlItemOnClickListener;
import com.alstudio.view.listener.AlDialogOnClickListener;
import com.loovee.imaohu.R;

/**
 * 微信分享 / 微信 、 朋友圈
 * 
 * @author jinx
 */
public class WeiXinShareDialog {

	/**
	 * 弹出选择分享到微信或者朋友圈的对话框
	 * 
	 * @param context
	 * @param content
	 * @param imagePath
	 * @param weixin
	 */
	public static void showDialog(final Activity context, final String content,
			final String imagePath, final WeiXin weixin) {

		String[] items = null;

		items = new String[] {
				context.getString(R.string.TxtWeixinShareToUser),
				context.getString(R.string.TxtWeixinShareToMoments), };

		final AlertDialogCreator dialog = AlertDialogCreator
				.createOneButtonWithListItemDialog(context, false, items);
		dialog.setCentralButtonName(context.getString(R.string.BtnCancel));

		dialog.setItemClickListener(new AlItemOnClickListener() {

			@Override
			public void OnClick(int position) {
				// TODO Auto-generated method stub
				doItemSelect(position, content, imagePath, weixin, context);

			}
		});

		// final AlertDialogUtil dialog = new AlertDialogUtil(context, true,
		// false, context.getString(R.string.share_to), items);
		//
		// dialog.setPostiveButtonName(context.getString(R.string.dialog_cancel));
		// dialog.setPostiveButtonListener(new DialogInterface.OnClickListener()
		// {
		//
		// @Override
		// public void onClick(DialogInterface d, int which) {
		//
		// dialog.dismissDialog();
		// }
		// });
		//
		// dialog.setItemListener(new DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface d, int which) {
		//
		// dialog.dismissDialog();
		// doItemSelect(which, content, imagePath, weixin, context);
		// }
		// });
		dialog.showDialog();

	}

	public static void shareToWeixinNormal(WeiXin weixin, Activity activity,
			String content, String imageFile) {
		doItemSelect(0, content, imageFile, weixin, activity);
	}

	private static void doItemSelect(int which, String content,
			String imagePath, WeiXin weixin, Activity context) {
		switch (which) {
		case 0:
			if (isWeixinInstall()) {
				// 分享至微信
				weixin.sharePersonaHomeWeiXin(content, imagePath,
						WeiXin.THUMB_SIZE, WeiXin.THUMB_SIZE);
			} else {
				showInstallWeixin(context);
			}
			break;
		case 1:
			if (isWeixinInstall()) {
				// 分享至微信朋友圈
				weixin.sharePersonaHomeWeiXinFri(content, imagePath,
						WeiXin.THUMB_SIZE, WeiXin.THUMB_SIZE);

			} else {

				showInstallWeixin(context);
			}
			break;
		}
	}

	public static void showInstallWeixin(final Context context) {

		final AlertDialogCreator mDialogCreator = AlertDialogCreator
				.createTwoButtonNormalDialog(context, false);
		mDialogCreator.setLeftKeyListener(new AlDialogOnClickListener() {

			@Override
			public void onClick() {
				// TODO Auto-generated method stub
				downWeixin(context);
			}
		});
		mDialogCreator.setMessage(context.getString(R.string.TxtWeixinInstallTips));

		mDialogCreator.showDialog();

	}

	public static void downWeixin(Context context) {
		Uri uri = Uri.parse(SnsConfig.WEIXIN_DOWNLOAD_URL);
		Intent it = new Intent(Intent.ACTION_VIEW, uri);
		context.startActivity(it);
	}

	public static boolean isWeixinInstall() {
		PackageManager pm = ALLocalEnv.getLocalEnv().getPackageManager();
		ApplicationInfo info = null;

		try {
			info = pm.getApplicationInfo("com.tencent.mm",
					PackageManager.GET_UNINSTALLED_PACKAGES);
		} catch (NameNotFoundException e) {
			info = null;
			ALLog.e(e.toString());
		}

		if (null != info) {
			return true;
		}
		return false;
	}
}
